home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / lib / python2.6 / lib2to3 / fixes / fix_import.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-04-20  |  2.9 KB  |  96 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. '''Fixer for import statements.
  5. If spam is being imported from the local directory, this import:
  6.     from spam import eggs
  7. Becomes:
  8.     from .spam import eggs
  9.  
  10. And this import:
  11.     import spam
  12. Becomes:
  13.     from . import spam
  14. '''
  15. from  import fixer_base
  16. from os.path import dirname, join, exists, pathsep
  17. from fixer_util import FromImport, syms, token
  18.  
  19. def traverse_imports(names):
  20.     '''
  21.     Walks over all the names imported in a dotted_as_names node.
  22.     '''
  23.     pending = [
  24.         names]
  25.     while pending:
  26.         node = pending.pop()
  27.         if node.type == token.NAME:
  28.             yield node.value
  29.             continue
  30.         if node.type == syms.dotted_name:
  31.             yield []([ ch.value for ch in node.children ])
  32.             []
  33.             continue
  34.         ''.join
  35.         if node.type == syms.dotted_as_name:
  36.             pending.append(node.children[0])
  37.             continue
  38.         if node.type == syms.dotted_as_names:
  39.             pending.extend(node.children[::-2])
  40.             continue
  41.         raise AssertionError('unkown node type')
  42.  
  43.  
  44. class FixImport(fixer_base.BaseFix):
  45.     PATTERN = "\n    import_from< 'from' imp=any 'import' ['('] any [')'] >\n    |\n    import_name< 'import' imp=any >\n    "
  46.     
  47.     def transform(self, node, results):
  48.         imp = results['imp']
  49.         if node.type == syms.import_from:
  50.             while not hasattr(imp, 'value'):
  51.                 imp = imp.children[0]
  52.             if self.probably_a_local_import(imp.value):
  53.                 imp.value = '.' + imp.value
  54.                 imp.changed()
  55.                 return node
  56.         else:
  57.             have_local = False
  58.             have_absolute = False
  59.             for mod_name in traverse_imports(imp):
  60.                 if self.probably_a_local_import(mod_name):
  61.                     have_local = True
  62.                     continue
  63.                 have_absolute = True
  64.             
  65.             if have_absolute:
  66.                 if have_local:
  67.                     self.warning(node, 'absolute and local imports together')
  68.                 
  69.                 return None
  70.             new = FromImport('.', [
  71.                 imp])
  72.             new.set_prefix(node.get_prefix())
  73.             return new
  74.         return self.probably_a_local_import(imp.value)
  75.  
  76.     
  77.     def probably_a_local_import(self, imp_name):
  78.         imp_name = imp_name.split('.', 1)[0]
  79.         base_path = dirname(self.filename)
  80.         base_path = join(base_path, imp_name)
  81.         if not exists(join(dirname(base_path), '__init__.py')):
  82.             return False
  83.         for ext in [
  84.             '.py',
  85.             pathsep,
  86.             '.pyc',
  87.             '.so',
  88.             '.sl',
  89.             '.pyd']:
  90.             if exists(base_path + ext):
  91.                 return True
  92.         
  93.         return False
  94.  
  95.  
  96.